package com.itgm.jiayun_web_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgm.jiayun_web_project.pojo.Activity;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activity")
@Slf4j
@Tag(name = "活动管理", description = "活动管理接口")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    //分页查询活动列表
    @GetMapping("/page")
    @Operation(summary = "分页查询活动列表")
    public Result<PageResult<Activity>> page(Integer page,
                                             Integer pageSize,
                                             @RequestParam(required = false) String name) {
        log.info("分页查询活动列表");
        Page<Activity> activityPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Activity> qw = new LambdaQueryWrapper<>();
        if (name != null) {
            qw.like(Activity::getActivityName, name);
        }
        Page<Activity> page1 = activityService.page(activityPage, qw);
        PageResult<Activity> pageResult = new PageResult<>();
        pageResult.setTotal(page1.getTotal());
        pageResult.setRecords(page1.getRecords());
        return Result.success(pageResult);
    }

    //新增 活动
    @PostMapping
    @Operation(summary = "新增活动")
    public Result<String> add(@RequestBody Activity activity) {
        log.info("新增活动");
        boolean save = activityService.save(activity);
        if (save) {
            return Result.success("新增活动成功");
        } else {
            return Result.error("新增活动失败");
        }
    }
}
